﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Services;
using System.Web.Script.Services;
using System.Text;

namespace TractRecord.Authenticated
{
    public partial class Settings : TractRecordBasePage 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            name.InnerHtml = CurrentUser.UserName;
            currentemail.InnerHtml = CurrentUser.Email;
            currentQuestion.InnerHtml = CurrentUser.PasswordQuestion;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static ArrayList UpdateUser(string CurrentPassword, string Password, string Email, string Question, string Answer)
        {
            ArrayList values = new ArrayList();

            bool retval = true;
            StringBuilder sb = new StringBuilder();

            if (!String.IsNullOrEmpty(Question) && !String.IsNullOrEmpty(Answer))
            {
                bool value = CurrentUser.ChangePasswordQuestionAndAnswer(CurrentPassword, Question, Answer);

                if (!value)
                {
                    sb.Append("An error occured changing the security question and answer.");
                }
                else
                {
                    Code.Common.Log("User account (" + CurrentUser.UserName + ") security question and answer updated.", "Audit");
                }

                retval = retval && value;
            }

            if (!String.IsNullOrEmpty(Password))
            {
                bool value = CurrentUser.ChangePassword(CurrentPassword, Password);

                if (!value)
                {
                    sb.Append("Old password does not match.");
                }
                else
                {
                    Code.Common.Log("User account (" + CurrentUser.UserName + ") password updated.", "Audit");
                }
                retval = retval && value;
            }

            if (!String.IsNullOrEmpty(Email))
            {
                CurrentUser.Email = Email;
                Membership.UpdateUser(CurrentUser);

                Code.Common.Log("User account (" + CurrentUser.UserName + ") email address updated.", "Audit");
            }

            if (retval)
            {
                values.Add(MembershipCreateStatus.Success.ToString());

                ListDictionary replacements = new ListDictionary();
                replacements.Add("<%Name%>", CurrentUser.UserName);

                WebCommon.GenerateEmailMessage(WebCommon.MailMessageType.AccountInformationUpdated, "User Account Information Updated", CurrentUser.Email, replacements);

            }
            else
            {
                values.Add(MembershipCreateStatus.ProviderError.ToString());
            }

            values.Add(sb.ToString());

            return values;
        }
    }
}